﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using context;
using entities;

namespace DataAccessLibrary.DAL
{
    public class PromotionDAO
    {
        public List<Promotion> GetListPromotion()
        {
            using (var ctx = new BlueMDataContext())
            {
                return ctx.Promotions.Where(p => p.PromotionEnd >= DateTime.Now).ToList();
            }
        }

        public List<PromotionDetail> GetPromotionDetails(int proID)
        {
            using (var ctx = new BlueMDataContext())
            {
                return ctx.PromotionDetails.Where(p => p.PromotionID == proID).ToList();
            }
        }

        public List<vSubPromotion> SelectPromotion(int available)
        {
            using (var ctx = new BlueMDataContext())
            {
                DateTime date = DateTime.Now;
                IQueryable<vSubPromotion> queryable = ctx.vSubPromotions;
                switch (available)
                {
                    case 1:
                        queryable = queryable.Where(p => p.PromotionEnd >= date);
                        break;
                    case 0:
                        queryable = queryable.Where(p => p.PromotionEnd < date);
                        break;
                }
                return queryable.ToList();
            }
        }

        public Promotion SelectPromotionById(int id)
        {
            using (var ctx = new BlueMDataContext())
            {
                return ctx.Promotions.First(p => p.PromotionID == id);
            }
        }

        public List<vSubPromotion> SelectPromotionByTime(DateTime begin, DateTime end)
        {
            using (var ctx = new BlueMDataContext())
            {
                return ctx.vSubPromotions.Where(p => p.PromotionStart >= begin && p.PromotionEnd <= end).ToList();
            }
        }

        public List<vSubPromotion> SelectPromotionByTitle(string title)
        {
            using (var ctx = new BlueMDataContext())
            {
                return ctx.vSubPromotions.Where(p => p.PromotionTitle.Contains(title)).ToList();
            }
        }

        public bool InsertPromotion(Promotion promotion)
        {
            using (var ctx = new BlueMDataContext())
            {
                ctx.Promotions.InsertOnSubmit(promotion);
                ctx.SubmitChanges();
            }

            return true;
        }

        public bool UpdatePromotion(Promotion promotion)
        {
            using (var ctx = new BlueMDataContext())
            {
                Promotion up = ctx.Promotions.First(p => p.PromotionID == promotion.PromotionID);
                up.PromotionEnd = promotion.PromotionEnd;
                up.PromotionStart = promotion.PromotionStart;
                up.PromotionDescription = promotion.PromotionDescription;
                up.PromotionTitle = promotion.PromotionTitle;
                if (promotion.PromotionBanner != null)
                    up.PromotionBanner = promotion.PromotionBanner;
                ctx.SubmitChanges();
            }

            return true;
        }

        public bool DeletePromotion(int promotionId)
        {
            using (var ctx = new BlueMDataContext())
            {
                Promotion dp = ctx.Promotions.First(p => p.PromotionID == promotionId);
                EntitySet<PromotionDetail> promotionDetails = dp.PromotionDetails;
                ctx.PromotionDetails.DeleteAllOnSubmit(promotionDetails);
                ctx.Promotions.DeleteOnSubmit(dp);
                ctx.SubmitChanges();
            }

            return true;
        }

        public List<vSubPromotionDetail> SelectPromotionDetailByPromotionId(int promotionId)
        {
            using (var ctx = new BlueMDataContext())
            {
                return ctx.vSubPromotionDetails.Where(p => p.PromotionID == promotionId).ToList();
            }
        }

        public PromotionDetail SelectPromotionDetail(int promotionDetailId)
        {
            using (var ctx = new BlueMDataContext())
            {
                return ctx.PromotionDetails.First(p => p.PromotionDetailID == promotionDetailId);
            }
        }


        public bool InsertPromotionDetail(int promotionId, string promotionContent, byte[] promotionImage)
        {
            var promotionDetail = new PromotionDetail
                                      {
                                          PromotionID = promotionId,
                                          PromotionContent = promotionContent,
                                          PromotionImage = promotionImage
                                      };
            using (var ctx = new BlueMDataContext())
            {
                ctx.PromotionDetails.InsertOnSubmit(promotionDetail);
                ctx.SubmitChanges();
            }

            return true;
        }

        public bool UpdatePromotionDetail(int promotionDetailId, string promotionContent, byte[] promotionImage)
        {
            using (var ctx = new BlueMDataContext())
            {
                PromotionDetail pd =
                    ctx.PromotionDetails.First(p => p.PromotionDetailID == promotionDetailId);
                pd.PromotionContent = promotionContent;
                if (promotionImage != null)
                {
                    pd.PromotionImage = promotionImage;
                }
                ctx.SubmitChanges();
            }

            return true;
        }

        public bool DeletePromotionDetail(int promotionDetailId)
        {
            using (var ctx = new BlueMDataContext())
            {
                PromotionDetail pd =
                    ctx.PromotionDetails.First(p => p.PromotionDetailID == promotionDetailId);
                ctx.PromotionDetails.DeleteOnSubmit(pd);
                ctx.SubmitChanges();
            }

            return true;
        }
    }
}